package com.faceye.core.service.impl;

import java.lang.reflect.Method;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import com.faceye.core.util.Json;

/**
 * 日志AOP
 * 
 * @author haipenge
 *
 */
@Aspect
@Component

public class LogAop {
	private Logger logger = LoggerFactory.getLogger(LogAop.class);

	@Pointcut("@annotation(com.faceye.core.service.Log)")
	public void logPointcut() {

	}

	@Pointcut("execution(* com.faceye..*.**(..))")
	public void logMethods() {

	}

	@Around("logPointcut() && logMethods()")
	public Object proceed(ProceedingJoinPoint joinPoint) throws Throwable {
		Object res = null;
		if (joinPoint != null) {
			StopWatch stopWatch = new StopWatch();
			String targetName=joinPoint.getTarget().getClass().getName();
			MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
			Method method = methodSignature.getMethod();
			String name = targetName+"."+method.getName();
			stopWatch.start(name);
			logger.trace(">>FaceYe --> aop:{}" , name);
			Object[] args = joinPoint.getArgs();
			if (args != null) {
				for (Object arg : args) {
					if (arg != null) {
						String json = Json.toJson(arg);
						logger.trace(">>FaceYe --> aop  arg is:{}" , json);
					}
				}
			}
			res = joinPoint.proceed();
			stopWatch.stop();
			logger.trace(">>FaceYe --aop:{}  watch:{}",name,stopWatch.prettyPrint());
		} else {
			logger.error(">>FaceYe --> error,joinPoint is null.");
		}
		return res;
	}
}
